import registnet.records.source.CurriculumSource;
import registnet.records.Curriculum;
import registnet.records.CurriculumCourse;

import resources.Constants;

/* [start] 	initializes gui variables */
String _curriculum = bundle.getProperty("registnet.module.student_records.macro.curriculum");

String _actions = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.actions");
String _export = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.actions.export");
String _print = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.actions.print");

String _course_code = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.course_code");
String _course_title = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.course_title");
String _prerequisite = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.prerequisite");
String _units = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.units");
String _year = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.year");
String _semester = bundle.getProperty("registnet.module.student_records.macro.curriculum.form.semester");
/* [end] */



/* [start]	initializes the year-semester hashmap */
Curriculum curriculum = new CurriculumSource(DAOStudent.getCurriculum().getCurriculumCode()).getCurriculum();
List courses = curriculum.getCourses();
List ascendingKey = new Vector();
HashMap yearSemester = new HashMap();
for(int i=0; i<courses.size(); i++){
	CurriculumCourse cc = courses.get(i);
	int[] y_s = new int[2];
	y_s[0] = cc.getSemester();
	y_s[1] = cc.getYear();
	String key;
		if(y_s[0] != 3)
			key = Constants.MAP_YEAR_VALUE.get(y_s[1]) + " " + _year + ", " + Constants.MAP_SEMESTER_VALUE.get(y_s[0]) + " " + _semester;
		else
			key = Constants.MAP_YEAR_VALUE.get(y_s[1]) + " " + _year + ", " + Constants.MAP_SEMESTER_VALUE.get(y_s[0]);
	if (!yearSemester.containsKey(key)){
		yearSemester.put(key, y_s);
		ascendingKey.add(key);
	}
}
/* [end] */



/* [start]	defines a renderer for the contents of the form5 */
private class CurriculumRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		if (data[1] != null){
			new Label(data[0]).setParent(row);
			new Label(data[1]).setParent(row);
			new Label(data[2]).setParent(row);
			new Label(data[3]).setParent(row);
		}
		else if(data[0].equals("")){
			Label label = new Label(data[0]);
			label.setParent(row);
			row.setSpans("4");
			row.setHeight("25px");
			row.setStyle("border-top: 2px solid #4f7fbf; background-color: #ffffff; ");
		}
		else{
			Label label = new Label(data[0]);
			label.setStyle("font-weight: bold");
			label.setParent(row);
			row.setSpans("4");
			row.setStyle("border-top: 2px solid #4f7fbf; border-bottom: 2px solid #4f7fbf; background-color: #BCD2EF;");
		}
	}
}
/* [end] */



/* [start]	retrieves the courses on the curriculum and places them on the grid */
void retrieveCourses(){
	ListIterator iter = ascendingKey.listIterator();
	String[][] listModel = new String[courses.size() + (2*yearSemester.size())][4];
	int i=0;
	while(iter.hasNext()){
		String key = iter.next();
		int[] y_s = yearSemester.get(key);
		listModel[i][0] = key + " (" + curriculum.getUnits(y_s[0], y_s[1]) + " " + _units + ")";
		i++;
		List courses = curriculum.getCourses(y_s[0], y_s[1]);
		for(int j=0; j<courses.size(); j++){
			CurriculumCourse course = courses.get(j);
			listModel[i][0] = course.getCourseCode();
			listModel[i][1] = course.getCourseName();
			String tmpPrerequisites = "";
			List prerequisites = course.getPrerequisites();
			for (int k=0; k<prerequisites.size(); k++){
				if (k == 0)
					tmpPrerequisites += prerequisites.get(k).getDescription();
				else
					tmpPrerequisites += " OR " + prerequisites.get(k).getDescription();
			}
			listModel[i][2] = tmpPrerequisites;
			listModel[i][3] = course.getUnit() + "";
			i++;
		}
		listModel[i][0] ="";
		i++;
	}	
	ListModel strset = new SimpleListModel(listModel);
	curriculum_g.setModel(strset);
	curriculum_g.setRowRenderer(new CurriculumRenderer());
}
/* [end] */